/*******************************************************************************
 * Copyright (c) 2004, 2005 Jean-Michel Lemieux, Jeff McAffer and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Prism is an RCP application developed for the book
 *     Eclipse Rich Client Platform -
 *         Designing, Coding, and Packaging Java Applications
 *         (http://eclipsercp.org)
 * Contributors:
 *     Jean-Michel Lemieux and Jeff McAffer - initial implementation
 *******************************************************************************/
package org.reliablesource.prism.product.jface;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.reliablesource.prism.ui.actions.OpenPreferencesAction;
import org.reliablesource.prism.ui.plugin.IErrorContext;
import org.reliablesource.prism.ui.viewers.ImageViewer;

public class PrismApplicationWindow extends ApplicationWindow implements IErrorContext {

	private ImageViewer imgViewer;

	// Actions
//	private ChatAction chatAction;
//	private AddContactAction addContactAction;
//	private AddGroupAction addGroupAction;
//	private LoginAction loginAction;
//	private RemoveContactAction removeContactAction;
//	private StatusActionGroup statusActions;
//	private Action doubleClickAction;
//	private RenameGroupOrEntryAction renameAction;
	private IAction exitAction;
	private OpenPreferencesAction openPrefsAction;

	public PrismApplicationWindow(Shell parentShell) {
		super(parentShell);
		addMenuBar();
		addStatusLine();
	}

	protected Control createContents(Composite parent) {
		getShell().setText("Prism");
//		getShell().setImage(PrismPlugin.getDefault().getImageRegistry().get(PrismUIConstants.IMG_ONLINE));
		getShell().setSize(275, 475);

		makeActions();
		imgViewer = new ImageViewer(parent);		
		initializeActions();
		
		addMenuBarEntries();
		addStatusLineEntries();
		addContextMenu();

		return imgViewer.getControl();
	}

	private void addContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				fillContextMenu(manager);
			}
		});
		Control control = imgViewer.getControl();
		Menu menu = menuMgr.createContextMenu(control);
		control.setMenu(menu);
	}

	private void fillContextMenu(IMenuManager manager) {
//		manager.add(chatAction);
		manager.add(new Separator());
//		manager.add(addContactAction);
//		manager.add(removeContactAction);
//		manager.add(addGroupAction);
//		manager.add(renameAction);
//		manager.add(new Separator());
//		manager.add(loginAction);
	}

	private void addStatusLineEntries() {
		StatusLineManager manager = getStatusLineManager();
//		manager.appendToGroup(StatusLineManager.END_GROUP, statusActions);
		manager.update(true);
	}

	private void addMenuBarEntries() {
		MenuManager menuBar = getMenuBarManager();
		IMenuManager fileMenu = new MenuManager("&Prism", "file");
		menuBar.add(fileMenu);
//		fileMenu.add(loginAction);
		fileMenu.add(new Separator());
//		fileMenu.add(chatAction);
		fileMenu.add(new Separator());
		fileMenu.add(exitAction);

		IMenuManager toolsMenu = new MenuManager("&Tools", "tools");
		menuBar.add(toolsMenu);
//		toolsMenu.add(addContactAction);

		IMenuManager contactsMenu = new MenuManager("&Manage Contacts", "contacts");
		toolsMenu.add(contactsMenu);
//		contactsMenu.add(addGroupAction);
//		contactsMenu.add(removeContactAction);
//		contactsMenu.add(renameAction);
		
		toolsMenu.add(new Separator());
		toolsMenu.add(openPrefsAction);

		menuBar.update(true);
	}

	private void makeActions() {
		Shell shell = getShell();

//		chatAction = new ChatAction(shell, session);
//
//		addContactAction = new AddContactAction(shell, session);
//		addContactAction.setAccelerator(SWT.CTRL | 'N');
//
//		removeContactAction = new RemoveContactAction(session);
//
//		addGroupAction = new AddGroupAction(shell, session);
//
//		loginAction = new LoginAction(shell, session);
//		loginAction.setAccelerator(SWT.F9);
//
//		
//
//		statusActions = new StatusActionGroup(shell, session);
//		session.addConnectionEstablishedListener(statusActions);
		
		exitAction = new Action("&Exit") {	
			public void run() {
				close();
			}
		};
		exitAction.setAccelerator(SWT.CTRL | 'X');
		
		openPrefsAction = new OpenPreferencesAction(shell);
	}
	
	private void initializeActions() {
		Shell shell = getShell();
//		ISelectionProvider selection = imgViewer.getSelectionProvider();
//		chatAction.setSelectionProvider(selection);

//		removeContactAction.setSelectionProvider(selection);
		
//		renameAction = new RenameGroupOrEntryAction(shell, imgViewer.getTree());
//		renameAction.setAccelerator(SWT.F2);
//		renameAction.setSelectionProvider(selection);
	}

	public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException {
	}

	public void setName(String title) {
		if (title == null)
			title = "";
		title += (title.length() == 0 ? "" : " - ") + "Prism";
		getShell().setText(title);
	}

	public void setImage(ImageDescriptor descriptor) {
		Image oldImage = null;
		Image newImage = descriptor == null ? null : descriptor.createImage();
		getShell().setImage(newImage);
		if (oldImage != null)
			oldImage.dispose();
	}
	
	public boolean close() {
//		statusActions.dispose();
		return super.close();
	}
}
